package com.ilop.sthome.mvp.present.device;

import android.content.Context;
import android.os.Handler;

import com.example.common.mvp.BasePresenterImpl;
import com.example.common.utils.SpUtil;
import com.ilop.sthome.common.command.SendDeviceCommand;
import com.ilop.sthome.data.event.EventRefreshDevice;
import com.ilop.sthome.data.greenDao.DeviceInfoBean;
import com.ilop.sthome.mvp.contract.device.ThermostatContract;
import com.ilop.sthome.utils.ConfigureUtil;
import com.ilop.sthome.utils.DeviceStatusUtil;
import com.ilop.sthome.utils.greenDao.utils.DeviceDaoUtil;
import com.ilop.sthome.utils.tools.ByteUtil;
import com.siterwell.familywellplus.R;

/**
 * @author skygge
 * @date 2020/4/21.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：
 */
public class ThermostatPresenter extends BasePresenterImpl<ThermostatContract.IView> implements ThermostatContract.IPresent {

    private Context mContext;
    private DeviceInfoBean mDevice;
    private SendDeviceCommand mSendEquipment;
    private Handler mHandler;

    public ThermostatPresenter(Context mContext) {
        this.mContext = mContext;
        mHandler = new Handler();
    }


    @Override
    public void getDeviceInfo(DeviceInfoBean deviceInfoBean) {
        this.mDevice = deviceInfoBean;
        DeviceInfoBean gateway = DeviceDaoUtil.getInstance().findGatewayByDeviceName(deviceInfoBean.getDeviceName());
        mSendEquipment = new SendDeviceCommand(mContext, gateway);
    }

    @Override
    public void refreshStatus(EventRefreshDevice event) {
        int new_device_id = event.getDevice_id();
        String new_status = event.getDevice_status();
        String new_type = event.getDevice_type();
        String new_name = event.getDeviceName();

        if (new_name.equals(mDevice.getDeviceName())
                && new_device_id == mDevice.getDevice_ID()
                && new_type.equals(mDevice.getDevice_type())) {
            mDevice.setDevice_status(new_status);
            mHandler.post(()-> analysisStatus(new_status));
        }
    }

    @Override
    public void analysisStatus(String status) {
        try {
            String statusStr = DeviceStatusUtil.getAlert(mContext, mDevice.getDevice_type(), status);
            int statusColor = DeviceStatusUtil.getStatus(mDevice.getDevice_type(), status);
            int res = ConfigureUtil.choseDeviceStateColor(statusColor);
            mView.showStatus(res, statusStr); //显示状态

            int quantity = Integer.parseInt(status.substring(2,4),16);
            int quantityRes = ConfigureUtil.choseQPic(quantity);
            String quantityStr = ConfigureUtil.choseLNum(quantity);
            mView.showQuantity(quantityRes, quantityStr);//显示电量

            byte d = (byte)Integer.parseInt(status.substring(0,2),16);
            int signalRes = ConfigureUtil.choseSPic(ByteUtil.convertByte2HexString((byte)(d&0x07)));
            mView.showSignal(signalRes);//显示信号

            byte ds = (byte)Integer.parseInt(status.substring(4,6),16);
            byte ds2 = (byte)Integer.parseInt(status.substring(6,8),16);
            byte ds3 = (byte)Integer.parseInt(status.substring(0,2),16);

            int currentMode = ((0x03) & (ds2)); //当前模式
            int decimal = (((byte)((0x20) & ds))==0?0:1);
            int sta =  ((0x1F) & ds);
            float setting_temp = ((float) sta)+(decimal== 0 ? 0f : 0.5f);//获取到设置温度
            showCurrentMode(currentMode, setting_temp);
            mView.showCurrentMode(currentMode);

            int lockStatus = (((byte)((0x20) & ds3))==0 ? 0 : 1); //铜锁状态
            mView.showLock(lockStatus);

            if(setting_temp>30.0f){
                mView.showOffLine();
            }else{
                saveModeTemp(currentMode, setting_temp);

                int currentTemp = ((0x3F) & (ds2>>2)); //当前温度
                mView.showCurrentTemp(setting_temp, currentTemp);

                int windowEnable = (((byte)((0x80) & ds3))==0 ? 0 : 1);//开窗使能
                byte windowStatus = (byte)((0x80) & ds); //开窗状态
                mView.showWindow(windowEnable, windowStatus);

                int valveEnable =  (((byte)((0x40) & ds3))==0 ? 0 : 1);//阀门使能
                byte valveStatus = (byte)((0x40) & ds); //阀门状态
                mView.showValve(valveEnable, valveStatus);
            }
        }catch (Exception e){
            mView.showOffLine();
            mView.showFunctionView(R.mipmap.timing_nor, R.mipmap.manual_sel, R.mipmap.freeze_nor, 0f, 2);
            mView.dismissTip();
        }
    }

    @Override
    public void sendCodeToSetting(String code) {
        mSendEquipment.sendEquipmentCommand(mDevice.getDevice_ID(), code);
    }

    @Override
    public String getSetFrom(int set_temp, int set_temp_decimal, int mode, int valve, int window, int lock) {
        byte [] ds= {0x00,0x00};
        ds[0]  = (byte)((window==0?0x00:0x80)|ds[0]);
        ds[0]  = (byte)((valve==0?0x00:0x40)|ds[0]);
        ds[0]  = (byte)((set_temp_decimal==0?0x00:0x20)|ds[0]);
        ds[0]  = (byte)((set_temp&0x1F)|ds[0]);

        ds[1]  = (byte)((lock==0?0x00:0x04)|ds[1]);
        ds[1]  = (byte)((mode & 0x03)|ds[1]);
        String str1 = ByteUtil.convertByte2HexString(ds[0]);
        String str2 = ByteUtil.convertByte2HexString(ds[1]);
        return ((str1+str2+"0000").toUpperCase());
    }

    private void showCurrentMode(int mode, float setting_temp){
        switch (mode){
            case 0:
                mView.showFunctionView(R.mipmap.timing_nor, R.mipmap.manual_nor, R.mipmap.freeze_sel, 15f, 0);
                mView.dismissTip();
                break;
            case 1:
                mView.showFunctionView(R.mipmap.timing_sel, R.mipmap.manual_nor, R.mipmap.freeze_nor, 30f, 1);
                mView.dismissTip();
                break;
            case 2:
                mView.showFunctionView(R.mipmap.timing_nor, R.mipmap.manual_sel, R.mipmap.freeze_nor, 30f, 2);
                mView.dismissTip();
                break;
            case 3:
                mView.showFunctionView(R.mipmap.timing_nor, R.mipmap.manual_sel, R.mipmap.freeze_nor, 30f, 2);
                mView.showTipDialog(setting_temp);
                break;
        }
    }

    private void saveModeTemp(int mode, float temp){
        if(mode==0){
            SpUtil.putString(mContext, "cold", String.valueOf(temp));
        }else if(mode == 1){
            SpUtil.putString(mContext, "auto", String.valueOf(temp));
        }else if(mode == 2){
            SpUtil.putString(mContext, "hand", String.valueOf(temp));
        }
    }
}
